package com.demostic.demostic_test.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.PointF;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.accloud.cloudservice.AC;
import com.accloud.cloudservice.PayloadCallback;
import com.accloud.service.ACDeviceMsg;
import com.accloud.service.ACException;
import com.accloud.service.ACMsg;
import com.accloud.service.ACObject;
import com.accloud.utils.LogUtil;
import com.badoo.mobile.util.WeakHandler;
import com.demostic.demostic_test.R;
import com.demostic.demostic_test.adapter.ManageMapAdapter901;
import com.demostic.demostic_test.base.BaseActivity;
import com.demostic.demostic_test.entity.DevicePropertyInfo_;
import com.demostic.demostic_test.entity.MapInfo;
import com.demostic.demostic_test.entity.Point;
import com.demostic.demostic_test.utils.DataUtils;
import com.demostic.demostic_test.utils.DeviceUtils;
import com.demostic.demostic_test.utils.DialogUtils;
import com.demostic.demostic_test.utils.MyLog;
import com.demostic.demostic_test.utils.SpUtils;
import com.demostic.demostic_test.utils.ToastUtils;
import com.glidebitmappool.GlideBitmapPool;
import com.google.gson.Gson;

import java.util.ArrayList;

public class MapManageActivity901 extends BaseActivity implements View.OnClickListener {
    public static final int DELAYED_FINISH = 1;
    public static final int DELAYED_REQUERY = 2;
    private final int DELMAP = 1;
    private int MapTag;
    private final String TAG = MapManageActivity901.class.getSimpleName();
    private final int USEMAP = 2;
    private ManageMapAdapter901 adapter;
    private Point charge_point;
    private Context context;
    private long deviceId;
    private ACDeviceMsg deviceMsg;
    private Dialog dialog;
    private WeakHandler handler = new WeakHandler(new Handler.Callback() {
        /* class com.demostic.demostic_test.activity.MapManageActivity901.AnonymousClass1 */

        @Override // android.os.Handler.Callback
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    DialogUtils.closeDialog(MapManageActivity901.this.dialog);
                    ToastUtils.showToast(MapManageActivity901.this.context, MapManageActivity901.this.getString(R.string.use_map_suc));
                    MapManageActivity901.this.finish();
                    return false;
                case 2:
                    DialogUtils.closeDialog(MapManageActivity901.this.dialog);
                    if (msg.arg1 == 1) {
                        ToastUtils.showToast(MapManageActivity901.this.context, MapManageActivity901.this.getString(R.string.delete_map_suc));
                    } else if (msg.arg1 == 2) {
                        ToastUtils.showToast(MapManageActivity901.this.context, MapManageActivity901.this.getString(R.string.save_map_suc));
                    }
                    MapManageActivity901.this.getDeviceProperty();
                    return false;
                default:
                    return false;
            }
        }
    });
    private byte[] idbyte;
    int index = 0;
    ArrayList<MapInfo> infoList;
    private int intent_mapid;
    private int itemIndex;
    private ImageView iv_back;
    private Dialog loadingDialog;
    private String physicalDeviceId;
    private RecyclerView recyclerview;
    private String serviceName;
    private byte[] slamBytes;
    private String subdomain;
    ArrayList<Integer> timelist;
    private Dialog tipsDialog;
    private TextView tv_nomap;
    private int workPattern;

    @Override // com.demostic.demostic_test.base.BaseActivity, android.support.v7.app.AppCompatActivity, android.support.v4.app.FragmentActivity, android.app.Activity
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map_manage);
        getDeviceInfo();
        initView();
        getIntentData();
        getDeviceProperty();
    }

    private void initView() {
        this.infoList = new ArrayList<>();
        this.timelist = new ArrayList<>();
        this.iv_back = (ImageView) findViewById(R.id.iv_back);
        this.iv_back.setOnClickListener(this);
        this.tv_nomap = (TextView) findViewById(R.id.tv_nomap);
        RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.map_manage);
        this.recyclerview = (RecyclerView) findViewById(R.id.recyclerview);
        this.adapter = new ManageMapAdapter901(this.context, this.infoList);
        this.adapter.setOnClickListener(new ManageMapAdapter901.OnClickListener() {
            /* class com.demostic.demostic_test.activity.MapManageActivity901.AnonymousClass2 */

            @Override // com.demostic.demostic_test.adapter.ManageMapAdapter901.OnClickListener
            public void onDelBtnClick(int position) {
                MapManageActivity901.this.initDealWithlMapDialog(1, position);
            }

            @Override // com.demostic.demostic_test.adapter.ManageMapAdapter901.OnClickListener
            public void onUseBtnClick(int position) {
                if (MapManageActivity901.this.infoList != null && MapManageActivity901.this.infoList.size() == 4 && position == 0) {
                    ToastUtils.showToast(MapManageActivity901.this.context, MapManageActivity901.this.getString(R.string.delete_map_first));
                } else if (position == 0 && MapManageActivity901.this.adapter.USE_TYPE == MapManageActivity901.this.adapter.CREATE) {
                    MapManageActivity901.this.getWorkStatus();
                } else if (position == 0 && MapManageActivity901.this.adapter.USE_TYPE == MapManageActivity901.this.adapter.SAVE) {
                    MapManageActivity901.this.itemIndex = position;
                    MapManageActivity901.this.MapTag = 2;
                    MapManageActivity901.this.doDeleteOrUseMap(position);
                } else {
                    MapManageActivity901.this.initDealWithlMapDialog(2, position);
                }
            }
        });
        this.recyclerview.setLayoutManager(new LinearLayoutManager(this.context));
        this.recyclerview.setAdapter(this.adapter);
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void getWorkStatus() {
        AC.deviceDataMgr().fetchCurrentProperty(this.subdomain, this.deviceId, new PayloadCallback<String>() {
            /* class com.demostic.demostic_test.activity.MapManageActivity901.AnonymousClass3 */

            public void success(String s) {
                MapManageActivity901.this.workPattern = ((DevicePropertyInfo_) new Gson().fromJson(s,  DevicePropertyInfo_.class)).getWork_pattern();
                MyLog.e(MapManageActivity901.this.TAG, "getWorkStatus success==:" + MapManageActivity901.this.workPattern);
                if (MapManageActivity901.this.workPattern == 9 || MapManageActivity901.this.workPattern == 17) {
                    MapManageActivity901.this.initDealWithlMapDialog(2, 0);
                } else {
                    ToastUtils.showToast(MapManageActivity901.this.context, MapManageActivity901.this.getString(R.string.set_robot_charging));
                }
            }

            @Override // com.accloud.cloudservice.BaseCallback
            public void error(ACException e) {
                MyLog.e(MapManageActivity901.this.TAG, "getWorkStatus error==:" + e.toString());
            }
        });
    }

    private void getIntentData() {
        Intent intent = getIntent();
        if (intent != null) {
            this.intent_mapid = intent.getIntExtra("CurrentMapid", 0);
            this.charge_point = new Point(intent.getIntExtra("ChargeBaseX", 0), intent.getIntExtra("ChargeBaseY", 0));
        }
    }

    private void getDeviceInfo() {
        this.context = this;
        this.physicalDeviceId = SpUtils.getSpString(this.context, "physicalDeviceId");
        this.subdomain = SpUtils.getSpString(this.context, "subdomain");
        this.deviceId = SpUtils.getLong(this.context, "deviceId");
        this.deviceMsg = new ACDeviceMsg();
        this.deviceMsg.setCode(98);
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void getDeviceProperty() {
        this.loadingDialog = DialogUtils.createLoadingDialog(this);
        AC.deviceDataMgr().fetchCurrentProperty(this.subdomain, this.deviceId, new PayloadCallback<String>() {
            /* class com.demostic.demostic_test.activity.MapManageActivity901.AnonymousClass4 */

            public void success(String s) {
                MapManageActivity901.this.index = 0;
                MapManageActivity901.this.timelist.clear();
                MapManageActivity901.this.infoList.clear();
                MapManageActivity901.this.idbyte = null;
                String map_save = ((DevicePropertyInfo_) new Gson().fromJson(s,  DevicePropertyInfo_.class)).getMap_save();
                if (!TextUtils.isEmpty(map_save)) {
                    MapManageActivity901.this.idbyte = Base64.decode(map_save, 0);
                    if (MapManageActivity901.this.idbyte != null && MapManageActivity901.this.idbyte.length > 0) {
                        int length = MapManageActivity901.this.idbyte.length;
                        for (int i = 0; i < length; i += 4) {
                            int curMapid = DataUtils.getMapID(MapManageActivity901.this.idbyte[i], MapManageActivity901.this.idbyte[i + 1], MapManageActivity901.this.idbyte[i + 2], MapManageActivity901.this.idbyte[i + 3]);
                            if (i == 0 || curMapid != 0) {
                                MapManageActivity901.this.timelist.add(Integer.valueOf(curMapid));
                            }
                            if (i == length - 4) {
                                MapManageActivity901.this.bubbleSort();
                                MapManageActivity901.this.sortByte(MapManageActivity901.this.idbyte);
                            }
                        }
                    }
                }
            }

            @Override // com.accloud.cloudservice.BaseCallback
            public void error(ACException e) {
                DialogUtils.closeDialog(MapManageActivity901.this.loadingDialog);
                ToastUtils.showErrorToast(MapManageActivity901.this.context, e.getErrorCode());
            }
        });
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void sortByte(byte[] bytes) {
        byte[] bt = new byte[bytes.length];
        System.arraycopy(bytes, 0, bt, 0, 4);
        for (int i = 4; i < bytes.length; i += 4) {
            System.arraycopy(bytes, i, bt, bt.length - i, 4);
        }
        System.arraycopy(bt, 0, this.idbyte, 0, bt.length);
    }

    public void bubbleSort() {
        int size = this.timelist.size();
        int[] ary = new int[size];
        for (int i = 0; i < size; i++) {
            ary[i] = this.timelist.get(i).intValue();
        }
        for (int i2 = 1; i2 < ary.length; i2++) {
            for (int j = 1; j < ary.length - i2; j++) {
                if (ary[j] < ary[j + 1]) {
                    int temp = ary[j];
                    ary[j] = ary[j + 1];
                    ary[j + 1] = temp;
                }
            }
        }
        this.timelist.clear();
        for (int i3 = 0; i3 < size; i3++) {
            this.timelist.add(Integer.valueOf(ary[i3]));
            if (i3 == size - 1) {
                getRecordMap(this.timelist.get(0).intValue());
            }
        }
    }

    private void getRecordMap(final int mapid) {
        ACMsg req = new ACMsg();
        req.setName("getRecordMap");
        req.put("device_id", Long.valueOf(this.deviceId));
        req.put("map_id", Integer.valueOf(mapid));
        this.serviceName = DeviceUtils.getServiceName(this.subdomain);
        AC.sendToService("", this.serviceName, 1, req, new PayloadCallback<ACMsg>() {
            /* class com.demostic.demostic_test.activity.MapManageActivity901.AnonymousClass5 */

            public void success(ACMsg resp) {
                LogUtil.e(MapManageActivity901.this.TAG, "MapEdit getRecordMap success resp:" + MapManageActivity901.this.index + "," + mapid + "," + resp.toString());
                if (MapManageActivity901.this.index == 0 && MapManageActivity901.this.timelist.get(0).intValue() == 0) {
                    MapManageActivity901.this.infoList.add(new MapInfo());
                    MapManageActivity901.this.index++;
                    MapManageActivity901.this.getRecordContinue();
                } else if (resp.get("data") != null) {
                    LogUtil.e(MapManageActivity901.this.TAG, "MapEdit getRecordMap success resp.data:" + MapManageActivity901.this.index + "," + resp.toString());
                    ACObject acObject = (ACObject) ((ArrayList) resp.get("data")).get(0);
                    String strMap = acObject.getString("clean_slam");
                    String roadData = acObject.getString("clean_road");
                    int xMax = acObject.getInt("slam_x_max");
                    int xMin = acObject.getInt("slam_x_min");
                    int yMax = acObject.getInt("slam_y_max");
                    int yMin = acObject.getInt("slam_y_min");
                    PointF rectMid = new PointF();
                    rectMid.set((float) ((xMax + xMin) / 2), (float) (1500 - ((yMax + yMin) / 2)));
                    if (!TextUtils.isEmpty(strMap)) {
                        MapManageActivity901.this.slamBytes = Base64.decode(strMap, 0);
                        MapInfo info = new MapInfo();
                        if (mapid == MapManageActivity901.this.intent_mapid) {
                            info.setLastP(MapManageActivity901.this.charge_point);
                        } else if (!TextUtils.isEmpty(roadData)) {
                            info.setLastP(DataUtils.getLastPoint(Base64.decode(roadData, 0)));
                        }
                        info.setSlamData(MapManageActivity901.this.slamBytes);
                        info.setMidP(rectMid);
                        info.setStart_time(acObject.getLong("start_time"));
                        info.setXmax(xMax);
                        info.setXmin(xMin);
                        info.setYmax(yMax);
                        info.setYmin(yMin);
                        MapManageActivity901.this.infoList.add(info);
                        MapManageActivity901.this.index++;
                        MapManageActivity901.this.getRecordContinue();
                    }
                    LogUtil.e(MapManageActivity901.this.TAG, "MapEdit getRecordMap success:" + MapManageActivity901.this.slamBytes.length + "," + xMax + "," + xMin + "," + yMax + "," + yMin + "---" + MapManageActivity901.this.infoList.size() + "---" + acObject.getLong("start_time"));
                } else {
                    MapManageActivity901.this.infoList.add(new MapInfo());
                    MapManageActivity901.this.index++;
                    MapManageActivity901.this.getRecordContinue();
                }
            }

            @Override // com.accloud.cloudservice.BaseCallback
            public void error(ACException e) {
                DialogUtils.closeDialog(MapManageActivity901.this.loadingDialog);
                ToastUtils.showErrorToast(MapManageActivity901.this.context, e.getErrorCode());
                MyLog.e(MapManageActivity901.this.TAG, "MapEdit getRecordMap error:" + e.toString());
            }
        });
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void getRecordContinue() {
        if (this.index < this.timelist.size()) {
            getRecordMap(this.timelist.get(this.index).intValue());
        }
        if (this.index == this.timelist.size()) {
            this.index = 0;
            DialogUtils.closeDialog(this.loadingDialog);
            this.adapter.notifyDataSetChanged();
        }
    }

    @Override // android.view.View.OnClickListener
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back /* 2131755323 */:
                finish();
                return;
            default:
                return;
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void initDealWithlMapDialog(final int mapTag, final int position) {
        View viewdel = LayoutInflater.from(this.context).inflate(R.layout.domap_dialog, (ViewGroup) null);
        this.tipsDialog = DialogUtils.showUnifyDialog(this.context, viewdel);
        this.tipsDialog.setCanceledOnTouchOutside(false);
        this.tipsDialog.show();
        TextView maptips_title = (TextView) viewdel.findViewById(R.id.maptips_title);
        TextView tv_explore_tips1 = (TextView) viewdel.findViewById(R.id.tv_explore_tips1);
        TextView tv_explore_tips2 = (TextView) viewdel.findViewById(R.id.tv_explore_tips2);
        if (mapTag == 1) {
            maptips_title.setText(getString(R.string.del_map_tips));
        } else if (mapTag == 2) {
            if (position == 0 && this.adapter.USE_TYPE == this.adapter.CREATE) {
                maptips_title.setText(getString(R.string.not_use_curmap));
                tv_explore_tips1.setVisibility(0);
                tv_explore_tips2.setVisibility(0);
            } else {
                maptips_title.setText(getString(R.string.use_map_tips));
            }
        }
        ((TextView) viewdel.findViewById(R.id.tv_dialog_cancel)).setOnClickListener(new View.OnClickListener() {
            /* class com.demostic.demostic_test.activity.MapManageActivity901.AnonymousClass6 */

            @Override // android.view.View.OnClickListener
            public void onClick(View v) {
                MapManageActivity901.this.tipsDialog.dismiss();
            }
        });
        ((TextView) viewdel.findViewById(R.id.tv_dialog_confirm)).setOnClickListener(new View.OnClickListener() {
            /* class com.demostic.demostic_test.activity.MapManageActivity901.AnonymousClass7 */

            @Override // android.view.View.OnClickListener
            public void onClick(View v) {
                MapManageActivity901.this.tipsDialog.dismiss();
                MapManageActivity901.this.itemIndex = position;
                MapManageActivity901.this.MapTag = mapTag;
                MapManageActivity901.this.doDeleteOrUseMap(position);
            }
        });
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void doDeleteOrUseMap(int position) {
        int startindex = position * 4;
        if (this.MapTag == 1) {
            if (position != 0 && DataUtils.getMapID(this.idbyte[startindex], this.idbyte[startindex + 1], this.idbyte[startindex + 2], this.idbyte[startindex + 3]) == DataUtils.getMapID(this.idbyte[0], this.idbyte[1], this.idbyte[2], this.idbyte[3])) {
                this.idbyte[0] = 0;
                this.idbyte[1] = 0;
                this.idbyte[2] = 0;
                this.idbyte[3] = 0;
            }
            setCurrentMapZero(startindex);
        } else if (this.MapTag != 2) {
        } else {
            if (position != 0) {
                System.arraycopy(this.idbyte, startindex, this.idbyte, 0, 4);
                toSend(this.idbyte);
            } else if (this.adapter.USE_TYPE == this.adapter.CREATE) {
                setCurrentMapZero(startindex);
            } else if (this.adapter.USE_TYPE == this.adapter.SAVE) {
                int size = this.infoList.size();
                byte[] bt = new byte[((size + 1) * 4)];
                System.arraycopy(this.idbyte, 0, bt, 0, this.idbyte.length);
                System.arraycopy(this.idbyte, 0, bt, size * 4, 4);
                toSend(bt);
            }
        }
    }

    private void setCurrentMapZero(int startindex) {
        this.idbyte[startindex] = 0;
        this.idbyte[startindex + 1] = 0;
        this.idbyte[startindex + 2] = 0;
        this.idbyte[startindex + 3] = 0;
        toSend(this.idbyte);
    }

    private void toSend(byte[] sendbt) {
        this.deviceMsg.setContent(sendbt);
        this.dialog = DialogUtils.createLoadingDialog_(this);
        this.dialog.show();
        sendToDeviceWithOption(this.deviceMsg, this.physicalDeviceId);
    }

    public void sendToDeviceWithOption(ACDeviceMsg deviceMsg2, String physicalDeviceId2) {
        AC.bindMgr().sendToDeviceWithOption(this.subdomain, physicalDeviceId2, deviceMsg2, 2, new PayloadCallback<ACDeviceMsg>() {
            /* class com.demostic.demostic_test.activity.MapManageActivity901.AnonymousClass8 */

            public void success(ACDeviceMsg deviceMsg) {
                if (MapManageActivity901.this.MapTag == 1) {
                    MapManageActivity901.this.reQuery(1);
                } else if (MapManageActivity901.this.MapTag != 2) {
                } else {
                    if (MapManageActivity901.this.itemIndex == 0) {
                        if (MapManageActivity901.this.adapter.USE_TYPE == MapManageActivity901.this.adapter.CREATE) {
                            MapManageActivity901.this.sendDeviceToExplore();
                        } else if (MapManageActivity901.this.adapter.USE_TYPE == MapManageActivity901.this.adapter.SAVE) {
                            MapManageActivity901.this.reQuery(2);
                        }
                        MapManageActivity901.this.adapter.USE_TYPE = 0;
                        return;
                    }
                    MapManageActivity901.this.handler.sendEmptyMessageDelayed(1, 2000);
                }
            }

            @Override // com.accloud.cloudservice.BaseCallback
            public void error(ACException e) {
                MyLog.e(MapManageActivity901.this.TAG, "sendToDeviceWithOption error " + e.toString());
                DialogUtils.closeDialog(MapManageActivity901.this.dialog);
                ToastUtils.showErrorToast(MapManageActivity901.this.context, e.getErrorCode());
            }
        });
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void sendDeviceToExplore() {
        ACDeviceMsg acDeviceMsg = new ACDeviceMsg();
        acDeviceMsg.setCode(70);
        acDeviceMsg.setContent(new byte[]{15});
        AC.bindMgr().sendToDeviceWithOption(this.subdomain, this.physicalDeviceId, acDeviceMsg, 2, new PayloadCallback<ACDeviceMsg>() {
            /* class com.demostic.demostic_test.activity.MapManageActivity901.AnonymousClass9 */

            public void success(ACDeviceMsg acDeviceMsg) {
                if (acDeviceMsg.getCode() == 70) {
                    MapManageActivity901.this.finish();
                }
            }

            @Override // com.accloud.cloudservice.BaseCallback
            public void error(ACException e) {
            }
        });
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void reQuery(int mapTag) {
        Message msg = new Message();
        msg.what = 2;
        msg.arg1 = mapTag;
        this.handler.sendMessageDelayed(msg, 5000);
    }

    /* access modifiers changed from: protected */
    @Override // android.support.v7.app.AppCompatActivity, android.support.v4.app.FragmentActivity, android.app.Activity
    public void onDestroy() {
        super.onDestroy();
        GlideBitmapPool.clearMemory();
    }
}
